<template>
	<view class="uni-common-pb uni-padding-wrap uni-common-mt">
		<view class="uni-form-item uni-column">
			<view class="title">账户</view>
			<input class="uni-input" v-model="username" placeholder="请输入账户名" />
		</view>
		<view class="uni-form-item uni-column">
			<view class="title">密码</view>
			<input class="uni-input" password type="text" v-model="password" placeholder="请输入密码" />
		</view>

		<checkbox-group @change="checkboxChange">
			<label>
				<checkbox value="cb" checked="true" />记住密码自动登录
			</label>
		</checkbox-group>
		<view class="uni-flex uni-row">
			<button type="primary" style="flex: 1;" @tap="loginTO">登陆</button>
			<view style="width: 20upx;"></view>
			<button type="default" plain="true" style="flex: 1;" @tap="register">注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: '',
				autologin: true,
				islogin: false,
				loading: false
			};
		},
		onShow(e) {

			//获取上次登录的账号
			uni.getStorage({
				key: 'username',
				success: (res) => {
					this.username = res.data;
				},
				fail: () => {

				}
			});

			// 上一次登录账号的密码
			uni.getStorage({
				key: 'password',
				success: (res) => {
					this.password = res.data;
					if (this.password && this.username) {
						this.loginTO();
					}
				},
				fail: () => {
					this.password = "";
				}
			});
		},
		onBackPress() {
			if (this.islogin) {
				return false;
			}
			uni.showToast({
				title: '请登录！',
				icon: 'none',
				mask: true,
				duration: 3000
			});
			return true;
		},
		methods: {
			checkboxChange: function(e) {
				this.autologin = false;
				if (e.detail.value == 'cb') {
					this.autologin = true;
				}
			},
			loginTO2() {
				this.username = this.username.toLowerCase();
				this.password = this.password.toLowerCase();

				uni.getStorage({
					key: this.username + '-password',
					success: (res) => {
						if (this.password != res.data) {
							uni.showToast({
								title: '错误的密码！',
								icon: 'none',
								mask: true,
								duration: 3000
							});
						} else {
							if (this.autologin) {
								uni.setStorage({
									key: 'password',
									data: this.password,
									success: function() {
										console.log('success');
									}
								});
							}
							uni.showToast({
								title: this.username + '已登录！',
								icon: 'none',
								mask: true,
								duration: 3000
							});
							this.islogin = true;
							uni.navigateBack();
						}
					},
					fail: () => {
						uni.showToast({
							title: '错误的用户名！',
							icon: 'none',
							mask: true,
							duration: 3000
						});
					}
				});

			},
			loginTO() {
				if (this.loading) {
					return;
				}
				this.loading = true;
				uni.showLoading({
					title: '登录中...'
				});
				console.log(this.url + '/api/feige/login?username=' + this.username + "&password=" + this.password);

				uni.request({
					url: this.url + '/api/feige/login',
					data: {
						"username": this.username,
						"password": this.password,
					},
					success: (res) => {
						console.log(JSON.stringify(res.data));

						if (res.data.code == 1) {
							uni.setStorage({
								key: "username",
								data: this.username,
								success: (res) => {

								},
								fail: () => {}
							});
							if (this.autologin) {
								uni.setStorage({
									key: "password",
									data: this.password,
									success: (res) => {

									},
									fail: () => {}
								});
							}
							var userinfo = res.data.data.password + ";" + res.data.data.mobile1 +
								";" + res.data.data.mobile2 + ";" + res.data.data.mobile3;
							var keys = userinfo.split(";", 4);
							console.log(keys[0] + ";" + keys[1]);
							uni.setStorage({
								key: this.username + "_mobile",
								data: userinfo,
								success: (res) => {

								},
								fail: () => {}
							});

							uni.showToast({
								title: this.username + '已登录！',
								icon: 'none',
								mask: true,
								duration: 3000
							});
							this.islogin = true;
							uni.navigateBack();
						} else {
							this.password = "";
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								mask: true,
								duration: 3000
							});
						}
					},
					fail: (err) => {
						console.log(JSON.stringify(err));
						uni.showToast({
							title: JSON.stringify(err),
							icon: 'none',
							mask: true,
							duration: 3000
						});
					},
					complete: () => {
						uni.hideLoading();
						this.loading = false;
					}
				});

			},
			register() {
				uni.navigateTo({
					url: '../register/register',
				});
			}
		},
	}
</script>

<style>
	button {
		margin-top: 30upx;
		margin-bottom: 30upx;
	}
</style>
